#include <stdio.h>
#include <stdlib.h>

void display();
void count();
void index();
void sort();
void append();
void reverse();
void delete(int x);
void pop();
void insert();	
void extend();	
	

int n;
int* A;

void main()
{
A=(int *)malloc(sizeof(int));
int i=1,choice,num;
printf("Enter no. of elements in initial list:");
scanf("%d", &n);

while(i<=n)
	{
	printf("\n Enter element : ");
        scanf("%d",&num);
        A =(int *)realloc(A,(i)*sizeof(int));
        *(A+i-1)=num;
	i++;
        }

display(A);
do
{
printf("1.Display list\n");
printf("2.Count list elements\n");
printf("3.Index of an element\n");
printf("4.Sort list\n");
printf("5.Append element\n");
printf("6.Reverse list\n");
printf("7.Delete an element\n");
printf("8.Pop element\n");
printf("9.Insert list\n");
printf("10.Extend element\n");
printf("Enter the operation you want to perform (input the number,enter %d to exit):\n",0);
scanf("%d",&choice);

	switch(choice)
	{
	case 0:exit;
	case 1:
		{
		display();
		break;
		}
	case 2:
		{
		count();
		break;
		}
	case 3:
		{
		index();
		display();
		break;
		}
	case 4:
		{
		sort();
		display();
		break;
		}
	case 5:	
		{
		append();
		display();
		break;
		}
	case 6:	
		{
		reverse();	
		display();
		break;
		}
	case 7:
		{int x;
		printf("Enter the element to be deleted :");
		scanf("%d",&x);
		delete(x);
		display();
		break;
		}
	
	case 8:	
		{
		pop();
		display();
		break;
		}
	case 9:
		{
		insert();	
		display();
		break;
		}	

	case 10:
		{
		extend();	
		display();
		break;
		}

	default:
		{
		printf("You entered wrong operation number\n");
		}
	}
}while(choice!=0);
}

void display()                              //DISPLAY 
{
printf("The list is : ");
int i;
if (n==0) printf("The list is empty,u may exit\n"); 
for(i=0;i<n;i++)
	printf("%8d",*(A+i));
printf("\n");	
}

void count()                             //COUNT
{
int i,num,cnt;
printf("Enter the element who's count u want : ");
scanf("%d", &num);
int count=0;
for(i=0; i<n; i++)
        {
	if(A[i]==num)
        cnt++;
        }
printf("%d appeared %d times.\n", num, cnt);
}

void index()                              //INDEX
{
	int i=1,num,flag=0;
	printf("Enter the number who's index u want:");
	scanf("%d",&num);
	for(i=1;i<=n;i++)
		{
		if (*(A+i-1)==num)
			{
			printf("The position is: %d \n",i);
			flag=1;
			}
		}	
		if (flag==0)
		printf("The number not in list: \n");
}

void sort()                               //SORT
{
int i,j,tmp;
for(i= 0; i < n; i++)
	{
		for(j = 0; j < n-1; j++)
      		{
			if(*(A+j) > *(A+j+1))
			{
        		tmp = *(A+j+1);
        		*(A+j+1) = *(A+j);
        		*(A+j) = tmp;
      			}
		}
	}
}

void append()                                   //APPEND
{
int num;
A =(int *)realloc(A,(++n)*sizeof(int));
printf("Enter the element to be appended :");
scanf("%d",&num);
*(A+n-1)=num;
}

void reverse()                                    //REVERSE
{
int i,tmp;
for(i=0; i<n/2; i++)
	{
	tmp=*(A+i);
	*(A+i)=*(A+n-1-i);
	*(A+n-1-i)=tmp;
	}
}

void delete(int x)                                    //DELETE
{
int flag=0;
int i,j;
	for(i=0;i<n;i++)
	{	
		if (*(A+i)==x)
		{
		for(j=i;j<n-1;j++)
			{
			*(A+j)=*(A+j+1);
			}
		A=(int *)realloc(A,(n-1)*(sizeof(int)));
		--n;
		flag=1;
		break;
		}
		 
	}
if (flag==0) printf("The element isn't in list \n");

}

void pop()                              //POP
{
int i,x,num;
printf("Enter the position from where u want to pop element :");
scanf("%d",&x); 
for(i=0;i<x;i++)
	{
	if (i==x-1)
		{
		num=*(A+i);
		delete((*(A+i)));
		break;
		}
	}
if (x<=n) printf("The element popped is: %d \n",num);
}

void insert()                                  //INSERT
{
int i,pos,num;
printf("Enter the position where you want to insert an element : \n");
scanf("%d", &pos);
printf("Enter the element you want to insert : \n");
scanf("%d", &num);

if(pos<1 || pos>n+1)
printf("Invalid position!\n");
else
{
A =(int *)realloc(A,(n++)*sizeof(int));

for(i=n-1; i>=pos; i--)
*(A+i)=*(A+i-1);
*(A+i)=num;        
}
}

void extend()                                     //EXTEND
{
int m,i=1,num;
printf("Enter the no. of elements in list to be extended  : ");
scanf("%d", &m);
int *B;
B=(int *)malloc(sizeof(int));
while(i<=m)
	{
	printf("\n Enter element : ");
        scanf("%d",&num);
        B =(int *)realloc(B,(i)*sizeof(int));
        *(B+i-1)=num;
	i++;
        }

int k;
for(k=0; k<m; k++)
        {
	A = (int *)realloc(A,(n+1)*sizeof(int));
        *(A+n)=*(B+k);
	n++;
        }

}